package com.kaguya.vams.controller;

import com.kaguya.vams.domain.Result;
import com.kaguya.vams.domain.entity.SysDict;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数字字典 控制层
 * @author : kaguya
 * @date : 10:27 2021/8/10
 */
@Slf4j
@RestController
@RequestMapping("/sys/dict")
public class SysDictController extends BaseController{

    //根据id获取子节点
    @PreAuthorize("hasAuthority('sys:dict:list')")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable Long id){

        List<SysDict> dictList = dictService.findChildData(id);
        return Result.ok(dictList);
    }

    //导入 批量添加
    @PreAuthorize("hasAuthority('sys:dict:import')")
    @PostMapping("/import")
    @Transactional
    public Result importDict(MultipartFile multipartFile) {
        log.info("数字字典 导入");
        dictService.importFile(multipartFile);
        return Result.ok();
    }

    //导出
    @PreAuthorize("hasAuthority('sys:dict:export')")
    @GetMapping("/export")
    public void exportDict(HttpServletResponse response) {
        log.info("数字字典 导出");
        //调用业务层 方法实现导出
        dictService.exportFile(response);
    }
}
